﻿using Lcygcc.Domain.Common;
using Lcygcc.Domain.Repository.Entity;
using Lcygcc.Domain.Repository.Entity.JKModel;
using Lcygcc.Domain.Service;
using Lcygcc.Infrastructure.Core;
using Lcygcc.Infrastructure.Logs;
using Lcygcc.Infrastructure.Repository;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lcygcc.Domain.TaskService.SyncToJKClound
{
    public class JKCloudChannel : BaseChannel, IBaseChannel
    {
        private readonly ISyncService m_SyncService = null;

        
        private readonly string mJKUrl = ""; //ConfigurationManager.AppSettings.Settings["CenterUri"].Value; ConfigurationManager.ConnectionStrings["OracleConnectString"].ToString();

        public JKCloudChannel(ISyncService syncService)
        {
            m_SyncService = syncService;
            mJKUrl = EnvironmentInfo.JKBaseUrl;
        }
        /// <summary>
        /// 发送到金科
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public override ReponseArgs SendToCloud(IBaseTaskService task)
        {
            List<SyncTemp> tempDataList = m_SyncService.GetTempData(0,4,0,200);
            if (tempDataList != null && tempDataList.Count > 0)
            {
                if (string.IsNullOrEmpty(mJKUrl))
                {
                    Logger.Info("-----JKCloudChannel.SendToCloud-----:" + "金科地址不能为空！");
                    return null;
                }
                List<SyncTemp> tempSucess = new List<SyncTemp>();
                List<SyncTemp> tempFaile = new List<SyncTemp>();
                //组装数据
                foreach (var tempData in tempDataList)
                {
                    string sendUrl = "";
                    var detail = CommonHelper.GetDicDetailInfo("enmPlateformToUrl", tempData.ServiceId);
                    if (detail != null)
                    {
                        sendUrl = Path.Combine(mJKUrl, detail.NisspCode).Replace("\\", "/");

                        //string reqParam = "";
                        string respones = HttpService.PostRequest(sendUrl,Encoding.UTF8, tempData.ProtocolData);
                        if (!string.IsNullOrEmpty(respones))
                        {
                            JKResponse model = JsonConvert.DeserializeObject<JKResponse>(respones);
                            if (model != null)
                            {
                                //成功
                                if (model.code == (int)enumJKReponse.OK)
                                {
                                    tempSucess.Add(tempData);
                                }
                                //地址不通，不处理
                                else if (model.code == (int)enumJKReponse.Error)
                                {

                                }
                                //返回报错
                                else
                                {
                                    tempData.FailMessage = respones;
                                    tempData.SendNum++;
                                    tempFaile.Add(tempData);
                                }
                            }
                            else
                            {
                                Logger.Info("-----JKCloudChannel.SendToCloud-----:" + "金科返回值反序列化失败：" + respones);
                            }
                        }
                    }
                    else
                    {
                        Logger.Info("-----JKCloudChannel.SendToCloud-----:" + "找不到对应的ServiceId：" + tempData.ServiceId);
                    }
                }
                if (tempSucess.Count > 0)
                {
                    m_SyncService.MoveTempToHistoryData(tempSucess);
                }

                if (tempFaile.Count > 0)
                {
                    m_SyncService.UpdateTempFail(tempFaile);
                }
            }
            return null;
        }
    }
}
